package com.walnut.lom_system.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.walnut.lom_system.common.annotation.DoLog;
import com.walnut.lom_system.common.lang.Const;
import com.walnut.lom_system.common.lang.OperateTypeContains;
import com.walnut.lom_system.common.lang.ResultInfo;
import com.walnut.lom_system.common.util.ResultInfoUtil;
import com.walnut.lom_system.domain.form.EmployInfoForm;
import com.walnut.lom_system.domain.form.EmployInfoSearchForm;
import com.walnut.lom_system.domain.vo.EmployInfoVo;
import com.walnut.lom_system.service.EmployInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;

/**
 * @author chenhetao
 * @version 1.0
 * @className AdminEmployController
 * @description TODO
 * @date 2022/4/17 00:10
 */
@RestController
@Api(tags = "招新及换届相关接口")
@RequestMapping("/employ")
public class EmployController {

    private static final Logger LOG = LoggerFactory.getLogger(HelloWorldController.class);

    @Autowired
    private EmployInfoService employInfoService;

    @PostMapping()
    @ApiOperation("报名者报名")
    @DoLog(operateModule = "OPERATE", operateType = OperateTypeContains.OPERATE_VISIT, operateDesc = "报名者报名")
    public ResultInfo addEmployee(@RequestBody @Validated EmployInfoForm employInfoForm) {
        int i = employInfoService.addEmployee(employInfoForm);
        if (i == Const.OP_SUCCESS) {
            return ResultInfoUtil.buildSuccess("添加成功");
        } else {
            return ResultInfoUtil.buildError("操作失败");
        }
    }

    @GetMapping("/{type}")
    @ApiOperation("获取本人报名信息")
    @DoLog(operateModule = "OPERATE", operateType = OperateTypeContains.OPERATE_VISIT, operateDesc = "获取本人报名信息")
    public ResultInfo<EmployInfoVo> queryEmployee(@PathVariable("type") int type) throws Exception {
        EmployInfoVo employInfoVo = employInfoService.getOwnEmployeeByType(type);
        return ResultInfoUtil.buildSuccess(employInfoVo);
    }


    @DeleteMapping("/{type}")
    @ApiOperation("删除本人指定类型的报名信息")
    @DoLog(operateModule = "OPERATE", operateType = OperateTypeContains.OPERATE_VISIT, operateDesc = "删除本人指定类型的报名信息")
    public ResultInfo deleteEmployee(@PathVariable("type") @NotNull int type) {
        //TODO 代码
        int i = employInfoService.deleteOwnEmployeeByType(type);
        if (i == Const.OP_SUCCESS){
            return ResultInfoUtil.buildSuccess("删除成功");
        }else {
            return ResultInfoUtil.buildError("未删除数据");
        }
    }

    @PutMapping("/{type}")
    @ApiOperation("更新本人报名信息")
    @DoLog(operateModule = "OPERATE", operateType = OperateTypeContains.OPERATE_VISIT, operateDesc = "更新操作")
    public ResultInfo updateEmployee(@PathVariable("type") int type, @RequestBody @Validated EmployInfoForm employInfoForm) {
        //TODO 代码
        int i = employInfoService.updateOwnEmployeeByType(type,employInfoForm);
        if (i == Const.OP_SUCCESS){
            return ResultInfoUtil.buildSuccess("修改成功，请重新查询获取");
        } else {
            return ResultInfoUtil.buildError("修改失败，无影响的数据行");
        }
    }
}
